package com.cxd.app.entity;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cxd.app.entity.base.BannerPictureBase;
import com.cxd.app.entity.base.LoanBase;
import com.cxd.base.util.BeanTools;
import com.cxd.base.util.PatternUtil;
import com.cxd.base.util.date.DateStyle;
import com.cxd.base.util.date.DateUtil;
import com.cxd.base.util.wtj.SUtils;
import com.cxd.message.exception.AppConstants;

@SuppressWarnings("serial")
public class AppLoan implements java.io.Serializable {

	/** 借款ID */
	private String id;
	/** 借款名称 */
	private String name;
	/** 借款人 */
	private String userId;
	/** 借款描述 */
	private String description;
	/** 借款用途 */
	private String loanPurpose;
	/** 实际借到的金额（放款金额），放款后才有值 */
	private String money;
	/** 预计的借款金额 */
	private String loanMoney;
	/** 投资递增金额 */
	private String cardinalNumber;
	/** 项目利率 */
	private String rate;
	/** 最小投资金额 */
	private String minInvestMoney;
	/** 还款方式 */
	private String repayType;
	/** 进度 */
	private String progress;
	/** 剩余时间 */
	private String remainTime;
	/** 投标记录 */
	// private List<Invest> invests = new ArrayList<Invest>(0);
	private String deadline;
	/** 预计计息时间 */
	private String exceptTime;
	/** 项目类型 */
	private String businessType;
	/** 项目状态 */
	private String status;
	/** 投资时间 */
	private Date investTime;
	/** 项目期限单位 */
	private String unit;
	/** 剩余可投金额 */
	private String remainMoney;
	/** 合同URL*/
	private String contractUrl;
	/**
	 * 投资ID
	 */
	private String investId;

	/**
	 * 还款计划
	 */
	private InvestInfo investInfo;
	/**
	 * 投资金额
	 */
	private String investMoney;
	/**
	 * 保全ID
	 */
	private String recordNo;
	/**
	 * 访问安存url
	 */
	private String anCunUrl;
	/**
	 * 是否进行安存保存
	 */
	private String isAnCun;
	public AppLoan() {

	}

	/**
	 * 
	 * @param loan
	 * @param bt
	 * @param type  1:简单返回  2:全量返回
	 */
	public AppLoan(LoanBase loan,BeanTools bt,int type) {
		this.id = loan.getId();
		this.name = loan.getName();
		this.userId = loan.getUserId();
		this.loanPurpose = loan.getLoanPurpose();
		this.money = loan.getMoney().toString();
		this.loanMoney = new DecimalFormat("#").format(loan.getLoanMoney());
		this.cardinalNumber = loan.getCardinalNumber().intValue()+"";
		this.rate = ((int) (loan.getRate() * 100)) + "";
		this.minInvestMoney = loan.getMinInvestMoney().intValue()+"";
		this.repayType = loan.getRepayType();
		this.deadline = loan.getDeadline().toString();
		this.exceptTime = DateUtil.dateToString(loan.getCommitTime(),DateStyle.YYYY_MM_DD.getValue());
		this.businessType = loan.getBusinessType();

		if(type==1){
			this.description = "";
		}
		if(type==2){
			this.description = SUtils.removeLabel(loan.getDescription());
			// 诚信理财计划
			String desc = loan.getDescription();
			if (desc.indexOf("诚信理财计划") >= 0) {
				// 20151213002751
				this.extractPlanningInfo(loan);
			} else {
				// 20151009001820
				this.extractLoanInfo(loan,bt);
			}
		}
	}

	// 标详情图片
	private List<Map<String, Object>> loanInfoPicUrls = new ArrayList<Map<String, Object>>();
	private List<Map<String, Object>> listLoanInfoList = new ArrayList<Map<String, Object>>();

	private List<Map<String, Object>> loanWindControlPicUrls = new ArrayList<Map<String, Object>>();
	private List<Map<String, Object>> listWindControlList = new ArrayList<Map<String, Object>>();

	/**
	 * 提取理财计划信息
	 * 
	 * @author wtj
	 * @date 2015年12月17日下午7:50:14
	 * @param loan
	 */
	private void extractPlanningInfo(LoanBase loan) {
		String descStr = loan.getDescription();
		String[] strArr = descStr.split("<tr");
		for (String str : strArr) {
			String[] thArr = str.split("</th>");
			if (thArr.length == 2) {
				Map<String, Object> loanInfoMap = new HashMap<String, Object>();

				loanInfoMap
						.put("key", thArr[0].replaceAll(
								">|<[^>]+>|class=\"[^\"]+\"| ", ""));
				loanInfoMap.put("value",
						thArr[1].replaceAll("<[^>]+>|&nbsp;", ""));
				if (loanInfoMap.get("key").toString().indexOf("项目风控") >= 0) {
					this.listWindControlList.add(loanInfoMap);
				} else {
					this.listLoanInfoList.add(loanInfoMap);
				}
			}
		}
	};

	/**
	 * 提取借款信息
	 * 
	 * @author wtj
	 * @date 2015年12月17日下午7:50:31
	 * @param loan
	 */
	private void extractLoanInfo(LoanBase loan,BeanTools bt) {
		// 获取项目详情图片
		List<String> picUrlsList = PatternUtil.getImaSrcUrl(loan
				.getDescription());
		for (String str : picUrlsList) {
			Map<String, Object> puMap = new HashMap<String, Object>();
			puMap.put("picUrl", AppConstants.PublicConstants.IMG_ROOT_URL + str);
			loanInfoPicUrls.add(puMap);
		}
		List<BannerPictureBase> fkPicList = bt
				.queryListBean(
						"select bp.* from loan_guarantee_pics lgp inner join banner_picture bp where lgp.pic_id = bp.id and lgp.loan_id = '"+loan.getId()+"' ORDER BY seq_num",
						BannerPictureBase.class);
		for(BannerPictureBase bean : fkPicList){
			Map<String, Object> puMap = new HashMap<String, Object>();
			puMap.put("picUrl", AppConstants.PublicConstants.IMG_ROOT_URL + bean.getPicture());
			loanWindControlPicUrls.add(puMap);
		}
		String descStr = loan.getDescription().replaceAll("<[^>]+>|&nbsp;", "");
		String[] strArr = descStr.split("【");
		for (String str : strArr) {
			String[] thArr = str.split("】");
			if (thArr.length == 2) {
				Map<String, Object> loanInfoMap = new HashMap<String, Object>();
				loanInfoMap.put("key", thArr[0]);
				loanInfoMap.put("value", thArr[1]);
				if (loanInfoMap.get("key").toString().indexOf("项目风控") >= 0
						|| loanInfoMap.get("key").toString().indexOf("抵押物") >= 0) {
					this.listWindControlList.add(loanInfoMap);
				} else {
					this.listLoanInfoList.add(loanInfoMap);
				}
			}
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getExceptTime() {
		return exceptTime;
	}

	public void setExceptTime(String exceptTime) {
		this.exceptTime = exceptTime;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getLoanPurpose() {
		return loanPurpose;
	}

	public void setLoanPurpose(String loanPurpose) {
		this.loanPurpose = loanPurpose;
	}


	public String getLoanMoney() {
		return loanMoney;
	}

	public void setLoanMoney(String loanMoney) {
		this.loanMoney = loanMoney;
	}

	public String getRate() {
		return rate;
	}

	public void setRate(String rate) {
		this.rate = rate;
	}


	public String getRepayType() {
		return repayType;
	}

	public void setRepayType(String repayType) {
		this.repayType = repayType;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}


	public String getMoney() {
		return money;
	}

	public void setMoney(String money) {
		this.money = money;
	}

	public String getCardinalNumber() {
		return cardinalNumber;
	}

	public void setCardinalNumber(String cardinalNumber) {
		this.cardinalNumber = cardinalNumber;
	}

	public String getMinInvestMoney() {
		return minInvestMoney;
	}

	public void setMinInvestMoney(String minInvestMoney) {
		this.minInvestMoney = minInvestMoney;
	}

	public String getProgress() {
		return progress;
	}

	public void setProgress(String progress) {
		this.progress = progress;
	}

	public String getDeadline() {
		return deadline;
	}

	public void setDeadline(String deadline) {
		this.deadline = deadline;
	}

	public String getRemainTime() {
		return remainTime;
	}

	public void setRemainTime(String remainTime) {
		this.remainTime = remainTime;
	}

	public String getBusinessType() {
		return businessType;
	}

	public void setBusinessType(String businessType) {
		this.businessType = businessType;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getUnit() {
		return unit;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public Date getInvestTime() {
		return investTime;
	}

	public void setInvestTime(Date investTime) {
		this.investTime = investTime;
	}

	public String getRemainMoney() {
		return remainMoney;
	}

	public void setRemainMoney(String remainMoney) {
		this.remainMoney = remainMoney;
	}

	public InvestInfo getInvestInfo() {
		return investInfo;
	}

	public void setInvestInfo(InvestInfo investInfo) {
		this.investInfo = investInfo;
	}


	public String getInvestMoney() {
		return investMoney;
	}

	public void setInvestMoney(String investMoney) {
		this.investMoney = investMoney;
	}

	public List<Map<String, Object>> getLoanInfoPicUrls() {
		return loanInfoPicUrls;
	}

	public void setLoanInfoPicUrls(List<Map<String, Object>> loanInfoPicUrls) {
		this.loanInfoPicUrls = loanInfoPicUrls;
	}

	public List<Map<String, Object>> getListLoanInfoList() {
		return listLoanInfoList;
	}

	public void setListLoanInfoList(List<Map<String, Object>> listLoanInfoList) {
		this.listLoanInfoList = listLoanInfoList;
	}

	public List<Map<String, Object>> getLoanWindControlPicUrls() {
		return loanWindControlPicUrls;
	}

	public void setLoanWindControlPicUrls(
			List<Map<String, Object>> loanWindControlPicUrls) {
		this.loanWindControlPicUrls = loanWindControlPicUrls;
	}

	public List<Map<String, Object>> getListWindControlList() {
		return listWindControlList;
	}

	public void setListWindControlList(
			List<Map<String, Object>> listWindControlList) {
		this.listWindControlList = listWindControlList;
	}

	public String getContractUrl() {
		return contractUrl;
	}

	public void setContractUrl(String contractUrl) {
		this.contractUrl = contractUrl;
	}

	public String getInvestId() {
		return investId;
	}

	public void setInvestId(String investId) {
		this.investId = investId;
	}

	public String getRecordNo() {
		return recordNo;
	}

	public void setRecordNo(String recordNo) {
		this.recordNo = recordNo;
	}

	public String getAnCunUrl() {
		return anCunUrl;
	}

	public void setAnCunUrl(String anCunUrl) {
		this.anCunUrl = anCunUrl;
	}

	public String getIsAnCun() {
		return isAnCun;
	}

	public void setIsAnCun(String isAnCun) {
		this.isAnCun = isAnCun;
	}
	
}
